package com.atguigu.gulimall.finance.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;
import com.atguigu.gulimall.finance.dao.TypeDao;
import com.atguigu.gulimall.finance.entity.TypeEntity;
import com.atguigu.gulimall.finance.entity.vo.TypeVo;
import com.atguigu.gulimall.finance.service.TypeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service("typeService")
public class TypeServiceImpl extends ServiceImpl<TypeDao, TypeEntity> implements TypeService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<TypeEntity> wrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        if (StrUtil.isNotEmpty(key)) {
            wrapper.like("type_name", key);
        }
        IPage<TypeEntity> page = this.page(
                new Query<TypeEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public TypeVo getTypeById(Long id) {
        TypeEntity type = this.getById(id);
        TypeVo typeVo = new TypeVo();
        BeanUtil.copyProperties(type, typeVo);
        typeVo.setTypeValue(type.getTypeValue().toString());
        return typeVo;
    }

}